/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package program.utilities;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import org.w3c.dom.Document;
import org.w3c.tidy.Tidy;
import org.xhtmlrenderer.pdf.ITextRenderer;


/**
 * Utility-class to write data on a pdf file
 * 
 * Responsible: Bernhard
 */
public class PDFUtilities {

    /**
     * Writes delivered html file to a pdf file
     * 
     * @param html file which should be written on a pdf file
     * @return pdf file
     */
    public static File createPDF(File html) throws Exception{
        
        File pdf = File.createTempFile("pdf", ".pdf");
        File xhtml = createxhtmlFile(html);

        String url = xhtml.toURI().toURL().toString();
        String outputFile = "firstdoc.pdf";
        OutputStream os = new FileOutputStream(pdf);
        ITextRenderer renderer = new ITextRenderer();
        renderer.setDocument(xhtml);
        renderer.layout();
        renderer.createPDF(os);

        os.close();

        return pdf;
    }

    /**
     * Writes delivered images to a pdf file
     * 
     * @param images images which should be written on a pdf file
     * @return pdf file
     */
    public static File createPDF(ArrayList<Image> images) throws Exception {
        File pdf = File.createTempFile("pdf", ".pdf");
        ArrayList<File> tempImages = createTempImages(images);
        File html = createHTMLFile(tempImages);
        File xhtml = createxhtmlFile(html);

        String url = xhtml.toURI().toURL().toString();
        String outputFile = "firstdoc.pdf";
        OutputStream os = new FileOutputStream(pdf);
        ITextRenderer renderer = new ITextRenderer();
        renderer.setDocument(xhtml);
        renderer.layout();
        renderer.createPDF(os);

        os.close();

        return pdf;
    }

    /**
     * Creates tempImage-files from deliverd images and returns them
     * 
     * @param images
     * @return tempImage-files
     */
    private static ArrayList<File> createTempImages(ArrayList<Image> images) throws IOException {
        ArrayList<File> tempImages = new ArrayList<>();

        for (int i = 0; i < images.size(); i++) {
            Image img = images.get(i);
            File tempImg = File.createTempFile("00" + i, ".jpeg");
            BufferedImage bufferedImage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
            Graphics2D g2 = bufferedImage.createGraphics();
            g2.drawImage(img, null, null);
            ImageIO.write(bufferedImage, "jpg", tempImg);
            tempImages.add(tempImg);
        }

        return tempImages;
    }

    /**
     * Creates HTML-file with delivered tempImage-files
     * 
     * @param tempImages
     * @return the HTML-file
     */
    private static File createHTMLFile(ArrayList<File> tempImages) throws Exception {
        File html = null;
        ArrayList<String> fileLines = new ArrayList<>();
        fileLines.add("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
        fileLines.add("<head>");
        fileLines.add("<title>");
        fileLines.add("PDF");
        fileLines.add("</title>");
        fileLines.add("</head>");
        fileLines.add("<html>");
        fileLines.add("<body>");
        for (File f : tempImages) {
            fileLines.add("<p>");
            fileLines.add("<img src=\"" + f.getName() + "\" width =\"700\" height = \"1000\"" + " alt=\"pic\"/>");
            fileLines.add("</p>");
        }
        fileLines.add("</body>");
        fileLines.add("</html>");

        html = File.createTempFile("htmlForPDF", ".html");
        FileWriter fw = new FileWriter(html);
        BufferedWriter bw = new BufferedWriter(fw);
        for (String s : fileLines) {
            bw.write(s);
            bw.newLine();
        }
        System.out.println(html.getPath());
        bw.close();

        return html;
    }

    /**
     * Creates XHTML-file with deliverd HTML-file
     * 
     * @param html
     * @return XHTML-file
     */
    private static File createxhtmlFile(File html) {
        File f = null;
        try {
            f = File.createTempFile("xhtml", ".xhtml");
            FileInputStream fis = new FileInputStream(html);
            FileOutputStream fos = new FileOutputStream(f);

            Tidy t = new Tidy();
            t.setShowWarnings(false);
            Document d = t.parseDOM(fis, fos);
        } catch (Exception ex) {
                        JOptionPane.showMessageDialog(null, ex);

        }

        return f;
    }

}